/**
 * Modules.js是3D库区图显示模型存放的地方
 *
 * @author 谢宁, Created on 2018-06-07
 */
/** ***************************************************************** */
//模型材质信息
var planeMat, RackMat, RackMat2, CargoMat, LineMat, RollTexture, RollMat, matArrayA;

var gPositionList = [];

var gro, mate, mesh, loader = new THREE.TextureLoader();

//region 放置天空盒
function addSkybox( size,scene ) {
    urls = [
        './ThreeJs/images/skybox/mountain_RT.jpg', // right
        './ThreeJs/images/skybox/mountain_LF.jpg', // left
        './ThreeJs/images/skybox/mountain_UP.jpg', // top
        './ThreeJs/images/skybox/mountain_DN.jpg', // bottom
        './ThreeJs/images/skybox/mountain_BK.jpg', // back
        './ThreeJs/images/skybox/mountain_FR.jpg'  // front
    ];
    var skyboxCubemap = new THREE.CubeTextureLoader().load( urls );
    skyboxCubemap.format = THREE.RGBFormat;

    var skyboxShader = THREE.ShaderLib['cube'];
    skyboxShader.uniforms['tCube'].value = skyboxCubemap;
    var obj = new THREE.Mesh(
        new THREE.BoxGeometry( size, size, size ),
        new THREE.ShaderMaterial({
            fragmentShader : skyboxShader.fragmentShader,
            vertexShader : skyboxShader.vertexShader,
            uniforms : skyboxShader.uniforms,
            depthWrite : false,
            side : THREE.BackSide
        })
    );
    scene.add( obj );
}

//region 滚动的物体
function addRollPlane(scene) {
    geo = new THREE.PlaneGeometry( 400, 20 );
    mesh = new THREE.Mesh( geo, RollMat );
    mesh.position.set(0,150,-690);
    scene.add( mesh );
}
//endregion

//region 矩形区域
function addPlane(x,z,width,length,scene) {
    var lineWidth = 8
    var geometry = new THREE.PlaneGeometry( lineWidth, length );
    var obj = new THREE.Mesh( geometry, LineMat );
    obj.position.set(x,1.5,z);
    obj.rotation.x = -Math.PI / 2.0;
    var obj2 = obj.clone();
    obj2.translateX(width);

    var geometry2 = new THREE.PlaneGeometry( lineWidth, width );
    var obj3 = new THREE.Mesh( geometry2, LineMat );
    obj3.position.set(x+width/2,1.5,z-length/2+lineWidth/2);
    obj3.rotation.x = -Math.PI / 2.0;
    obj3.rotation.z = -Math.PI / 2.0;
    var obj4 = obj3.clone();
    obj4.translateX(length-lineWidth);

    var group = new THREE.Group();
    group.add(obj);
    group.add(obj2);
    group.add(obj3);
    group.add(obj4);
    group.translateX(-width/2);
    scene.add( group );
}
//endregion

//region 库区
/** 放置虚线框区域和库区名称 */
function addArea(x,z,width,length,scene,name,textColor,font_size,textposition) {
    addPlane(x,z,width,length,scene);

    new THREE.FontLoader().load('./ThreeJs/FZYaoTi_Regular.json',function(font){
        ////加入立体文字
        var text= new THREE.TextGeometry(name,{
            // 设定文字字体
            font:font,
            //尺寸
            size:font_size,
            //厚度
            height:0.01
        });
        text.computeBoundingBox();
        //3D文字材质
        mate = new THREE.MeshStandardMaterial({color:"#" + textColor});
        mesh = new THREE.Mesh(text,mate)
		// mesh.position.z = z + length/2 - 10;
		// mesh.position.x = x - 15;
  //       if(textposition == "左对齐"){
  //           mesh.position.x = x - width/2 + 10;
  //       }else if(textposition == "居中"){
  //           mesh.position.x = x - 15;
  //       }else if(textposition == "右对齐"){
  //           mesh.position.x = x + width/2 - 60;
  //       }else if(textposition == "上居中"){
		// 	mesh.position.x = x - 15;
		// 	mesh.position.z = z - length/2 + 50;
		// }else if(textposition == "上右对齐"){
		// 	mesh.position.x = x + width/2 - 150;
		// 	mesh.position.z = z - length/2 + 50;
		// }
        mesh.position.set(x - 15,1.3,z + length/2 - 10);
        mesh.rotation.x = -Math.PI/2;
		var mesh2 = mesh.clone();
        mesh2.position.set(x + 15,1.3,z - length/2 + 10);
		mesh2.rotation.z= Math.PI;
		var mesh3 = mesh.clone();
        mesh3.position.set(x + width/2-10,1.3,z+15);
		mesh3.rotation.z= Math.PI/2;
		var mesh4 = mesh.clone();
        mesh4.position.set(x - width/2+10,1.3,z-15);
		mesh4.rotation.z= -Math.PI/2;
        scene.add(mesh);
        scene.add(mesh2);
        scene.add(mesh3);
        scene.add(mesh4);
    });
}

function formNum(key){
	if(10>key){
		return "00"+key;
	}else if(100>key){
		return "0"+key;
	}else if(100<=key && 1000>key){
		return key;
	}
}


	//创建墙壁
	function createCubeWall(width, height, depth, angle, material, x, y, z, name,scene) {
		geo = new THREE.BoxGeometry(width, height, depth);
		mesh = new THREE.Mesh(geo, material);
		mesh.position.x = x;
		mesh.position.y = y;
		mesh.position.z = z;
		mesh.rotation.y += angle * Math.PI; //-逆时针旋转,+顺时针
		mesh.name = name;
		scene.add(mesh);
	}
	//墙壁材料初始化
	function initMatArry(){
		matArrayA.push(new THREE.MeshPhongMaterial({color: 0xafc0ca}));  //前  0xafc0ca :灰色
		matArrayA.push(new THREE.MeshPhongMaterial({color: 0xafc0ca}));  //后
		matArrayA.push(new THREE.MeshPhongMaterial({color: 0xd6e4ec}));  //上  0xd6e4ec： 偏白色
		matArrayA.push(new THREE.MeshPhongMaterial({color: 0xd6e4ec}));  //下
		matArrayA.push(new THREE.MeshPhongMaterial({color: 0xafc0ca}));  //左    0xafc0ca :灰色
		matArrayA.push(new THREE.MeshPhongMaterial({color: 0xafc0ca}));  //右
		
		planeMat = new THREE.MeshLambertMaterial();
		RackMat = new THREE.MeshLambertMaterial();
		RackMat2 = new THREE.MeshPhongMaterial({color:0x1C86EE});
		CargoMat = new THREE.MeshLambertMaterial();
		LineMat = new THREE.MeshLambertMaterial();
		RollMat = new THREE.MeshLambertMaterial();
	
		new THREE.TextureLoader().load( './ThreeJs/images/plane.png', function( map ) {
			planeMat.map = map;
			planeMat.transparent = true;
			planeMat.opacity = 0.8;
			planeMat.needsUpdate = true;
		} );
		new THREE.TextureLoader().load( "./ThreeJs/images/rack.png", function( map ) {
			RackMat.map = map;
			RackMat.needsUpdate = true;
		} );
		new THREE.TextureLoader().load( "./ThreeJs/images/box.png", function( map ) {
			CargoMat.map = map;
			CargoMat.needsUpdate = true;
		} );
		new THREE.TextureLoader().load( "./ThreeJs/images/line.png", function( map ) {
			LineMat.map = map;
			LineMat.needsUpdate = true;
		} );
		RollTexture = new THREE.TextureLoader().load( "./ThreeJs/images/biaoyu.png", function( map ) {
			RollMat.map = map;
			RollMat.needsUpdate = true;
			RollMat.transparent = true;
			RollMat.side = THREE.DoubleSide;
		} );
		RollTexture.wrapS = THREE.RepeatWrapping;
		RollTexture.wrapT=THREE.RepeatWrapping;
		
	}
	//创建地面及墙壁
	function createFloorAndWall(x,y,z,scene) {
        gro = new THREE.BoxGeometry(x,1,z);
        mate= new THREE.MeshBasicMaterial({color:0x9e9e9e})
        mesh = new THREE.Mesh(gro, mate);
        mesh.name = "地面";
        scene.add(mesh);
		createCubeWall(10, y, z, 0, matArrayA, x/2+5, y/2, 0, "墙面1",scene);
		createCubeWall(10, y, z, 1, matArrayA, -x/2-5, y/2, 0, "墙面2",scene);
		createCubeWall(10, y, x, -0.5, matArrayA, 0, y/2, z/2-5, "墙面3",scene);  
		createCubeWall(10, y, x, 0.5, matArrayA, 0, y/2, -z/2+5, "墙面4",scene);  
    }
	//创建库区
	function createPart(partlist,scene) {
		for(var i = 0 ; i < partlist.length ; i++){
			var o = partlist[i];
			addArea(o.px,o.pz,o.x,o.z,scene,o.name,"FF0000",20,o.pz>0?"上居中":"居中");
			createshelves(o.x,o.z,o.px,o.pz,o.shelveslist,scene,o.name);
		}
	}
	//创建坐标轴
	function createAxis(x,y,z,scene){
		for(var i = 0 ; i < 3 ; i ++){
			if(0 == i){
				gro = new THREE.BoxGeometry(x,1,1);
			}else if(1 == i){
				gro = new THREE.BoxGeometry(1,y*2,1);
			}else if(2 == i){
				gro = new THREE.BoxGeometry(1,1,z);
			}
			mate= new THREE.MeshBasicMaterial({color:0xff0000});
			mesh = new THREE.Mesh(gro, mate);
			mesh.name = "坐标轴"+(i+1);
			scene.add(mesh);
		}
	}
	//创建货架
	function createshelves(partx,partz,partpx,partpz,shelveslist,scene,pname){
		var num = shelveslist.length;
		//货位长宽 shelvesx,shelvesz
		var shelvesx = (partx/(1+num*2)).toFixed(2);
		var shelvenum = 0,shelvesz = 0;
		for(var i=0;i<num;i++){
			shelvenum = shelvenum > shelveslist[i].cnum ? shelvenum : shelveslist[i].cnum ;
		}
		shelvesz = (partz/(shelvenum+1)).toFixed(2);
		var positionx = partpx+partx/2 - shelvesx*3/2;
		var positiony1 = partpz+partz/2 - shelvesz/2;
		var positiony2 = partpz-partz/2 + shelvesz/2;
		for(var i=0;i<num;i++){
			if(shelveslist[i].istop){
				addShelf(positionx,60,positiony2,shelvesx,2,shelvesz,2,60,2,scene,pname+shelveslist[i].name,shelveslist[i].cnum,shelveslist[i].lnum,shelvesz)				
			}else{
				addShelf(positionx,60,positiony1,shelvesx,2,shelvesz,2,60,2,scene,pname+shelveslist[i].name,shelveslist[i].cnum,shelveslist[i].lnum,-shelvesz)
			}
			positionx += -shelvesx*2;
		}
	}
	function addShelf(x,y,z,plane_x,plane_y,plane_z,holder_x,holder_y,holder_z,scene,name,cnum,lnum,zChange) {
		addPlane(x,z+zChange*cnum/2,plane_x,plane_z*cnum,scene);
		if(lnum <= 1){
				addRack(x,2,z,plane_x,plane_y,plane_z,holder_x,2,holder_z,scene,name+"-"+formNum(1),cnum,zChange);
		}else{
			for(var i = 0; i < lnum; i++){
				addRack(x,y*(i+1),z,plane_x,plane_y,plane_z,holder_x,holder_y,holder_z,scene,name+"-"+formNum(i+1),cnum,zChange);
			}
		}
	}
	//创建货位，及货架支柱
	function addRack(x,y,z,plane_x,plane_y,plane_z,holder_x,holder_y,holder_z,scene,name,num,zChange) {
	    var plane = new THREE.BoxGeometry( plane_x, plane_y, plane_z );
	    var gz = [];
	    for(var i = 0; i < num; i++){
	        gz.push( z + zChange/2 + (zChange)*i );
	        var obj = new THREE.Mesh( plane, RackMat );
	        obj.position.set(x , y, gz[i]) ;
			var gname = name+"-"+formNum(i+1);
	        obj.name = "货位-"+gname;
	        scene.add(obj);
			addGoods(x,y+25,gz[i],plane_x*0.8,50,plane_z*0.8,scene,'货物-'+gname);
	    }
	
	    var holder = new THREE.BoxGeometry( holder_x, holder_y, holder_z );
		for(var i=0;i<=num;i++){
			var obj2 = new THREE.Mesh( holder, RackMat2, 0 );
			var obj3 = new THREE.Mesh( holder, RackMat2, 0 );
			obj2.position.set(x-plane_x/2+holder_x/2,y-holder_y/2-plane_y/2,z-holder_z/2+zChange*i);
			obj3.position.set(x+plane_x/2-holder_x/2,y-holder_y/2-plane_y/2,z-holder_z/2+zChange*i);
			if(i == 0){
				obj2.position.set(x-plane_x/2+holder_x/2,y-holder_y/2-plane_y/2,z-holder_z/2+zChange*i+2);
				obj3.position.set(x+plane_x/2-holder_x/2,y-holder_y/2-plane_y/2,z-holder_z/2+zChange*i+2);
			}
			scene.add(obj2);
			scene.add(obj3);	
		}
	}
	//添加货物
	function addGoods(x,y,z,box_x,box_y,box_z,scene,name) {
		x = x.toFixed(2);
		y = y.toFixed(2);
		z = z.toFixed(2);
		box_x = box_x.toFixed(2);
		box_y = box_y.toFixed(2);
		box_z = box_z.toFixed(2);
	    geo = new THREE.BoxGeometry( box_x, box_y, box_z );
	    mesh = new THREE.Mesh( geo, CargoMat );
	    mesh.position.set(x,y,z);
		var gPosition = {name:name,x:box_x,y:box_y,z:box_z,px:x,py:y,pz:z,istrue:true};
		gPositionList.push(gPosition);
	    mesh.name = name;
	    scene.add(mesh);
	}
	//创建几何体
function SGeometry(x,y,z,px,py,pz,name,scene,mate){
	geo = new THREE.BoxGeometry(x,y,z);
	if(mate.color){
		material = new THREE.MeshBasicMaterial({color: mate.color});
		mesh = new THREE.Mesh(geo, material);
		mesh.position.set(px,py,pz);
		mesh.name = name;
		scene.add(mesh);
	}else{
		loader.load(mate.path, function(texture) {
			texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
			texture.repeat.set(mate.repeatx,mate.repeaty);
			material = new THREE.MeshBasicMaterial({map: texture});
			mesh = new THREE.Mesh(geo, material);
			mesh.position.set(px,py,pz);
			mesh.name = name;
			scene.add(mesh);
		});
	}
}	
	

